package com.smart.sms.service.impl;

import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.smart.sms.config.AliSmsProperties;
import com.smart.sms.service.AliSmsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 新手  核心参数不校验
 * 幂等性校验
 */
@Service
@Slf4j
public class AliSmsServiceImpl implements AliSmsService {
    @Resource
    Client client;
    @Resource
    AliSmsProperties properties;

    @Override
    public void sendCode(String phone, String code) {
        SendSmsRequest request = new SendSmsRequest();
        // 手机号
        request.phoneNumbers = phone;
        request.templateParam = code;

        // 短信平台后台生成
        request.signName = properties.getSignName();
        request.templateCode = properties.getTemplateCode();

        try {
            SendSmsResponse response = client.sendSms(request);
            if ("success".equals(response.body.getCode())) {
                log.info(response.body.message);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
